/**
 * @param {number[]} nums
 * @return {number}
 */
var majorityElement = function(nums) {
    if(nums.length==1)return nums[0]
    let num = nums.length/2
    let obj = {}
    for (const val of nums) {
        if(obj[val]){
            obj[val] += 1
            if(obj[val]>num) return val
        }else{
            obj[val] = 1
        }
    }

};


// 抵消算法
// 简单理解就是 因为最后的众数超过了一半, 所以不管怎么抵消, 最后都是众数可以笑到最后
;(function(){
  //return
  //[1,1,2,2,2]
const majorityElement = nums => {
    let count = 1;
    let majority = nums[0];
    for (let i = 1; i < nums.length; i++) {
        if (count === 0) {
            majority = nums[i];
        }
        if (nums[i] === majority) {
            count++;
        } else {
            count--;
        }
    }
    return majority;
};

})();